<template>
    <div class="article-lists">
        <el-card class="!border-none" shadow="never">

            <el-tabs v-model="queryParams.show_type" @tab-change="getLists">
              <el-tab-pane
                  v-for="(item, index) in tabsMap"
                  :key="index"
                  :label="item.name"
                  :name="item.type"
                  lazy
              ></el-tab-pane>
            </el-tabs>
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="标题">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.name"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="内容状态">
                  <el-select class="w-[280px]" v-model="queryParams.is_enable">
                    <el-option label="全部" value />
                    <el-option label="显示" :value="1" />
                    <el-option label="隐藏" :value="0" />
                  </el-select>
                </el-form-item>
<!--                <el-form-item label="展现形式">
                  <el-select class="w-[280px]" v-model="queryParams.show_type">
                    <el-option label="全部" value />
                    <el-option label="渠道对接" :value="1" />
                    <el-option label="产品库" :value="2" />
                  </el-select>
                </el-form-item>-->
                <el-form-item label="发布时间">
                  <daterange-picker
                      v-model:startTime="queryParams.start_time"
                      v-model:endTime="queryParams.end_time"
                  />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" shadow="never">
            <div>
                <router-link
                    v-perms="['article.banner/add', 'article.banner/add:edit']"
                    :to="{
                        path: getRoutePath('article.banner/add:edit'),
                        query:{
                          show_type:queryParams.show_type,
                        }
                    }"
                >
                    <el-button type="primary" class="mb-4">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        发布
                    </el-button>
                </router-link>
            </div>
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="序号(ID)" prop="id" min-width="80" />
                <el-table-column label="封面" min-width="100">
                    <template #default="{ row }">
                        <image-contain
                            v-if="row.banner_url"
                            :src="row.banner_url"
                            :width="60"
                            :height="45"
                            :preview-src-list="[row.banner_url]"
                            preview-teleported
                            fit="contain"
                        />
                    </template>
                </el-table-column>
                <el-table-column
                    label="标题"
                    prop="name"
                    min-width="160"
                    show-tooltip-when-overflow
                />

                <el-table-column
                    label="创建人"
                    prop="create_admin_desc"
                    min-width="160"
                    show-tooltip-when-overflow
                />
                <el-table-column label="内容状态" prop="is_enable_desc" min-width="100" />
                <el-table-column label="排序" prop="sort" min-width="100" />
                <el-table-column label="发布时间" prop="create_time" min-width="180" />
                <el-table-column label="操作" width="180" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['article.banner/edit', 'article.banner/add:edit']"
                            type="primary"
                            link
                        >
                            <router-link
                                :to="{
                                    path: getRoutePath('article.banner/add:edit'),
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >
                                编辑
                            </router-link>
                        </el-button>
                        <el-button
                            v-perms="['article.banner/delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>

                        <el-switch
                            v-perms="['article.banner/updateStatus']"
                            v-model="row.is_enable"
                            :active-value="1"
                            :inactive-value="0"
                            @change="changeStatus($event, row.id)"
                        />
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup name="bannerLists">
import { bannerLists, bannerDelete, bannerStatus } from '@/api/article'
import { usePaging } from '@/hooks/usePaging'
import { getRoutePath } from '@/router'
import feedback from '@/utils/feedback'


enum ShowTypeEnum {
   CHANNEL = 1,
   PRODUCT = 2,
   OFFICEPLACE = 4
}


const queryParams = reactive({
    name: '',
    is_enable: '',
    show_type: 0,
    start_time: '',
    end_time: ''
})

queryParams.show_type = ShowTypeEnum.CHANNEL
const tabsMap = [
  {
    name: '渠道对接',
    type: ShowTypeEnum.CHANNEL
  },
  {
    name: '产品库',
    type: ShowTypeEnum.PRODUCT
  },
  {
    name: '助融之家',
    type: ShowTypeEnum.OFFICEPLACE
  }
]

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: bannerLists,
    params: queryParams
})
const changeStatus = async (is_enable: any, id: number) => {
    try {
        await bannerStatus({ id, is_enable })
        getLists()
    } catch (error) {
        getLists()
    }
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await bannerDelete({ id })
    getLists()
}

onActivated(() => {
    getLists()
})

//跳回到原tab处
/*const route = useRoute();
queryParams.show_type = parseInt((route.query.show_type as string));*/
getLists()
</script>
